// *************************************
// EFMP Fleet
// Table4.do
// *************************************

/*
// Description
// ----------------------------
This program generates table 4.
*/

/*** START CODE ***/

*Load in GPM by Vehicle Type, generated as part of Table 1*
import delimited using "$Data/gpm_data.csv", clear

gen ev = 0
	replace ev= 1 if veh_type=="BEV"|veh_type=="PHEV_EV"
gen phev = 0
	replace phev = 1 if veh_type == "PHEV_EV" | veh_type == "PHEV_ICE" 
gen phev_weight = .
	replace phev_weight = .55 if veh_type == "PHEV_EV"
		replace phev_weight = .45 if veh_type == "PHEV_ICE"
gen efmp = 0
	replace efmp = 1 if ev ==1 | phev == 1 | veh_type == "Hybrid"

forvalues z = 1/3 {

preserve

*************
*Assumptions*
*************

*ICE VMT amount
local ice 9800
local ice_gpm .0402

if `z' == 1 {
*VMT first scenario = EFMP vehicle gets full ICE mileage
gen vmt = `ice'
	replace vmt = . if veh_type == "EFMP_avg"
	}
*VMT second scenario = EFMP vehicle uses only the Davis mileage, gap mileage disappears
else if `z' >= 2 {
gen vmt = `ice'
	replace vmt = 6300 if veh_type=="BEV"
	replace vmt = 7800 if veh_type=="PHEV_EV" | veh_type=="PHEV_ICE" | veh_type=="PHEV_AVG"
	replace vmt = 9800 if veh_type=="Hybrid"
	replace vmt = . if veh_type == "EFMP_avg"
	}
	
	
	
	
*EV BTUs/Gallon
gen btu_gallon = 115 if ev == 1
*EV KWH/thousandBTU
gen kwh_btu = .000293071*1000 if ev == 1
*ICE C02/Gallon
gen ice_co2_gallon = 8.887
*Natural Gas lb CO2 per kwh -> kg co2 per kwh
gen ng_co2_kwh = .92/2.20462


***********
**Metrics**
***********
	
*Gallons of fuel equivalent fuel burned
gen gallons = vmt * gpm

*****
*EV Numbers
*****
gen btu = btu_gallon * gallons
gen kwh = btu * kwh_btu

*100% NG emissions
gen co2_emission = kwh * ng_co2_kwh
	*For ICEs
	replace co2_emission = gallon * ice_co2_gallon if ev==0
	egen co2_phev = wtmean(co2_emission), weight(phev_weight)
	replace co2_emission = co2_phev if veh_type == "PHEV_AVG"
		drop co2_phev

		

*Generate average emission and vmt for the average EFMP vehicle using subsidy counts as the weights

*For Panels A and B we use the VMT as calculated in the VMT formula
if `z' ==1 | `z' == 2 {
		egen efmp_emission = wtmean(co2_emission), weight(efmp_count)
			replace co2_emission = efmp_emission if veh_type == "EFMP_avg"
		egen efmp_vmt = wtmean(vmt), weight(efmp_count)
			replace vmt = efmp_vmt if veh_type == "EFMP_avg" 
				tempfile hello`z'
				save `hello`z''
			drop efmp*
	
	}
	
*For panel C, we assume the difference between the EFMP and ICE mileage is supplanted by ICE emissions
else if `z' ==3 {
	egen efmp_emission = wtmean(co2_emission), weight(efmp_count)
	egen efmp_vmt = wtmean(vmt), weight(efmp_count)
	gen ice_vmt = `ice'
	gen vmt_diff = ice_vmt - efmp_vmt
	
	gen replacement_emission = vmt_diff * `ice_gpm' * ice_co2_gallon
	*replace co2_emission = ((efmp_emission * efmp_vmt)+(replacement_emission * vmt_diff))/(efmp_vmt+vmt_diff) if veh_type == "EFMP_avg"
	replace co2_emission = efmp_emission + replacement_emission if veh_type == "EFMP_avg"
	tempfile hello3
	save `hello3'
	}
*************************************************************
*Scenario****************************************************
*************************************************************

*Compare EFMP Alternative Vehicle to different power generation scenario




		keep if veh_type == "EFMP_avg" | veh_type == "Counterfactual" | veh_type == "New_car" | veh_type == "All_cars_bought"
		keep veh_type co2_emission
			gen i = 1
		reshape wide co2, i(i) j(veh_type) string

		foreach i in Counterfactual New_car All_cars_bought {
		gen saving_100_`i' = co2_emission`i' - co2_emissionEFMP_avg
			summ saving_100_`i'
			local `i'_100_`z' = string(r(mean),"%10.0fc")
		gen saving_50_`i' = co2_emission`i' - co2_emissionEFMP_avg/2
			summ saving_50_`i'
			local `i'_50_`z' = string(r(mean),"%10.0fc")
			}

restore
}
	
	use `hello3', clear
	
	*Tex Table of Results
	capture erase "$EFMPFleetRes/Implied_CO2_savings.tex"
	file open fh using "$EFMPFleetRes/Implied_CO2_savings.tex", write replace
	
	*Header
	file write fh ///
	"\begin{tabular}{l c c c}"  _n  ///
	"\toprule" _n ///
	" & \multicolumn{3}{c}{Reference Vehicle} \\ " _n ///
	" \cline{2-4} \\ " _n ///
	" &   & \multicolumn{1}{c}{Avg. new car} &  \multicolumn{1}{c}{Avg. new or used}  \\  " _n  ///
	"\multicolumn{1}{c}{Electric Generation Source}  & \multicolumn{1}{c}{Counterfactual} & \multicolumn{1}{c}{purchased} &  \multicolumn{1}{c}{car purchased}  \\  " _n  ///
	"\cline{1-4} \\ " _n
	
	*Panel A 	
	file write fh "\textit{Panel A: EV VMT = NHTS ICE} \\ " _n
	file write fh "\hspace{5 pt} Natural Gas  & `Counterfactual_100_1' & `New_car_100_1' & `All_cars_bought_100_1' \\ " _n
	file write fh "\hspace{5 pt}  50\% Natural Gas, 50\% Renewable  & `Counterfactual_50_1' & `New_car_50_1' & `All_cars_bought_50_1' \\ " _n
	file write fh " \\ " _n
	*Panel B 	
	file write fh "\textit{Panel B: EV VMT = NHTS EV VMT} \\ " _n
	file write fh "\hspace{5 pt} Natural Gas  & `Counterfactual_100_2' & `New_car_100_2' & `All_cars_bought_100_2' \\ " _n
	file write fh "\hspace{5 pt}  50\% Natural Gas, 50\% Renewable  & `Counterfactual_50_2' & `New_car_50_2' & `All_cars_bought_50_2' \\ " _n
	file write fh " \\ " _n
	*Panel C 	
	file write fh "\textit{Panel C: EV VMT = NHTS; Remainder NHTS ICE} \\ " _n
	file write fh "\hspace{5 pt} Natural Gas  & `Counterfactual_100_3' & `New_car_100_3' & `All_cars_bought_100_3' \\ " _n
	file write fh "\hspace{5 pt}  50\% Natural Gas, 50\% Renewable  & `Counterfactual_50_3' & `New_car_50_3' & `All_cars_bought_50_3' \\ " _n
	
	file write fh " \\ " _n
	file write fh "Reference Vehicle MPG & 35.0 & 27.48 & 24.87 \\ " _n
	file write fh "Reference Vehicle GPM & .0286 & .0364 & .0402 \\ " _n
	
	
	*Bottom 
	file write fh ///
	"\bottomrule  " _n ///
	"\end{tabular}"  _n _n _n _n _n
	
file close fh
macro drop fh











********************************************************
*********** DO FOR BEV ONLY ****************************

*Load in GPM by Vehicle Type*
import delimited using "$file/gpm_data.csv", clear


gen ev = 0
	replace ev= 1 if veh_type=="BEV"|veh_type=="PHEV_EV"
gen phev = 0
	replace phev = 1 if veh_type == "PHEV_EV" | veh_type == "PHEV_ICE" 
gen phev_weight = .
	replace phev_weight = .55 if veh_type == "PHEV_EV"
		replace phev_weight = .45 if veh_type == "PHEV_ICE"
gen efmp = 0
	replace efmp = 1 if ev ==1 | phev == 1 | veh_type == "Hybrid"
	
	
drop if veh_type == "PHEV_EV" | veh_type == "PHEV_ICE" | veh_type == "PHEV_AVG"  | veh_type == "Hybrid" | veh_type == "EFMP_avg"


forvalues z = 1/3 {

preserve
*************
*Assumptions*
*************

*ICE VMT amount
local ice 9800
local ice_gpm .0402

if `z' == 1 {
*VMT first scenario = EFMP vehicle gets full ICE mileage
gen vmt = `ice'
	replace vmt = . if veh_type == "EFMP_avg"
	}
*VMT second scenario = EFMP vehicle uses only the Davis mileage, gap mileage disappears or is on ICE
else if `z' >= 2 {
gen vmt = `ice'
	replace vmt = 6300 if veh_type=="BEV"
	replace vmt = . if veh_type == "EFMP_avg"
	}
	
	
	
	
*EV BTUs/Gallon
gen btu_gallon = 115 if ev == 1
*EV KWH/thousandBTU
gen kwh_btu = .000293071*1000 if ev == 1
*ICE C02/Gallon
gen ice_co2_gallon = 8.887
*Natural Gas lb CO2 per kwh -> kg co2 per kwh
gen ng_co2_kwh = .92/2.20462


***********
**Metrics**
***********
	
*Gallons of fuel equivalent fuel burned
gen gallons = vmt * gpm

*****
*EV Numbers
*****
gen btu = btu_gallon * gallons
gen kwh = btu * kwh_btu

*100% NG emissions
gen co2_emission = kwh * ng_co2_kwh
	*For ICEs
	replace co2_emission = gallon * ice_co2_gallon if ev==0

		

*Generate average emission and vmt for the average EFMP vehicle using subsidy counts as the weights

*For Panels A and B we use the VMT as calculated in the VMT formula
if `z' ==1 | `z' == 2 {
		*egen efmp_emission = wtmean(co2_emission), weight(efmp_count)			
		*egen efmp_vmt = wtmean(vmt), weight(efmp_count)	
		tempfile hello`z'
		save `hello`z''
	}
	
*For panel C, we assume the difference between the EFMP and ICE mileage is supplanted by ICE emissions
else if `z' ==3 {
	egen efmp_emission = wtmean(co2_emission), weight(efmp_count)
	egen efmp_vmt = wtmean(vmt), weight(efmp_count)
	gen ice_vmt = `ice'
	gen vmt_diff = ice_vmt - efmp_vmt
	
	gen replacement_emission = vmt_diff * `ice_gpm' * ice_co2_gallon
	*replace co2_emission = ((efmp_emission * efmp_vmt)+(replacement_emission * vmt_diff))/(efmp_vmt+vmt_diff) if veh_type == "EFMP_avg"
	replace co2_emission = efmp_emission + replacement_emission if veh_type == "BEV"
	tempfile hello`z'
	save `hello`z''
	}
*************************************************************
*Scenario****************************************************
*************************************************************

*Compare EFMP Alternative Vehicle to different power generation scenario
		keep veh_type co2_emission
			gen i = 1
		reshape wide co2, i(i) j(veh_type) string

		foreach i in Counterfactual New_car All_cars_bought {
		gen bev_100_`i' = co2_emission`i' - co2_emissionBEV
			summ bev_100_`i'
			local `i'_100_`z'_bev = string(r(mean),"%10.0fc")
		gen bev_50_`i' = co2_emission`i' - co2_emissionBEV/2
			summ bev_50_`i'
			local `i'_50_`z'_bev = string(r(mean),"%10.0fc")
			}

restore
}
use `hello3', clear

	*Tex Table of Results
	capture erase "$EFMPFleetRes/Implied_CO2_savings_bev.tex"
	file open fh using "$EFMPFleetRes/Implied_CO2_savings_bev.tex", write replace
	
	*Header
	file write fh ///
	"\begin{tabular}{l c c c}"  _n  ///
	"\toprule" _n ///
	" & \multicolumn{3}{c}{Reference Vehicle} \\ " _n ///
	" \cline{2-4} \\ " _n ///
	" &   & \multicolumn{1}{c}{Avg. new car} &  \multicolumn{1}{c}{Avg. new or used}  \\  " _n  ///
	"\multicolumn{1}{c}{Electric Generation Source}  & \multicolumn{1}{c}{Counterfactual} & \multicolumn{1}{c}{purchased} &  \multicolumn{1}{c}{car purchased}  \\  " _n  ///
	"\cline{1-4} \\ " _n
	
	*Panel A 	
	file write fh "\textit{Panel A: EV VMT = NHTS ICE} \\ " _n
	file write fh "\hspace{5 pt} Natural Gas  & `Counterfactual_100_1_bev' & `New_car_100_1_bev' & `All_cars_bought_100_1_bev' \\ " _n
	file write fh "\hspace{5 pt}  50\% Natural Gas, 50\% Renewable  & `Counterfactual_50_1_bev' & `New_car_50_1_bev' & `All_cars_bought_50_1_bev' \\ " _n
	file write fh " \\ " _n
	*Panel B 	
	file write fh "\textit{Panel B: EV VMT = NHTS EV VMT} \\ " _n
	file write fh "\hspace{5 pt} Natural Gas  & `Counterfactual_100_2_bev' & `New_car_100_2_bev' & `All_cars_bought_100_2_bev' \\ " _n
	file write fh "\hspace{5 pt}  50\% Natural Gas, 50\% Renewable  & `Counterfactual_50_2_bev' & `New_car_50_2_bev' & `All_cars_bought_50_2_bev' \\ " _n
	file write fh " \\ " _n
	*Panel C 	
	file write fh "\textit{Panel C: EV VMT = NHTS; Remainder NHTS ICE} \\ " _n
	file write fh "\hspace{5 pt} Natural Gas  & `Counterfactual_100_3_bev' & `New_car_100_3_bev' & `All_cars_bought_100_3_bev' \\ " _n
	file write fh "\hspace{5 pt}  50\% Natural Gas, 50\% Renewable  & `Counterfactual_50_3_bev' & `New_car_50_3_bev' & `All_cars_bought_50_3_bev' \\ " _n
	
	file write fh " \\ " _n
	file write fh "Reference Vehicle MPG & 35.0 & 27.48 & 24.87 \\ " _n
	file write fh "Reference Vehicle GPM & .0286 & .0364 & .0402 \\ " _n
	
	
	*Bottom 
	file write fh ///
	"\bottomrule  " _n ///
	"\end{tabular}"  _n _n _n _n _n
	
file close fh
macro drop fh





**************************************************

*PHEV
**************************************************

*Load in GPM by Vehicle Type*
import delimited using "$file/gpm_data.csv", clear

gen ev = 0
	replace ev= 1 if veh_type=="BEV"|veh_type=="PHEV_EV"
gen phev = 0
	replace phev = 1 if veh_type == "PHEV_EV" | veh_type == "PHEV_ICE" 
gen phev_weight = .
	replace phev_weight = .55 if veh_type == "PHEV_EV"
		replace phev_weight = .45 if veh_type == "PHEV_ICE"
gen efmp = 0
	replace efmp = 1 if ev ==1 | phev == 1 | veh_type == "Hybrid"

drop if veh_type == "BEV" | veh_type == "Hybrid" | veh_type == "EFMP_avg"	
	
	
forvalues z = 1/3 {

preserve
*************
*Assumptions*
*************

*ICE VMT amount
local ice 9800
local ice_gpm .0402

if `z' == 1 {
*VMT first scenario = EFMP vehicle gets full ICE mileage
gen vmt = `ice'
	replace vmt = . if veh_type == "EFMP_avg"
	}
*VMT second scenario = EFMP vehicle uses only the Davis mileage, gap mileage disappears
else if `z' >= 2 {
gen vmt = `ice'
	replace vmt = 6300 if veh_type=="BEV"
	replace vmt = 7800 if veh_type=="PHEV_EV" | veh_type=="PHEV_ICE" | veh_type=="PHEV_AVG"
	replace vmt = 9800 if veh_type=="Hybrid"
	replace vmt = . if veh_type == "EFMP_avg"
	}
	
	
	
	
*EV BTUs/Gallon
gen btu_gallon = 115 if ev == 1
*EV KWH/thousandBTU
gen kwh_btu = .000293071*1000 if ev == 1
*ICE C02/Gallon
gen ice_co2_gallon = 8.887
*Natural Gas lb CO2 per kwh -> kg co2 per kwh
gen ng_co2_kwh = .92/2.20462


***********
**Metrics**
***********
	
*Gallons of fuel equivalent fuel burned
gen gallons = vmt * gpm

*****
*EV Numbers
*****
gen btu = btu_gallon * gallons
gen kwh = btu * kwh_btu

*100% NG emissions
gen co2_emission = kwh * ng_co2_kwh
	*For ICEs
	replace co2_emission = gallon * ice_co2_gallon if ev==0
	egen co2_phev = wtmean(co2_emission), weight(phev_weight)
	replace co2_emission = co2_phev if veh_type == "PHEV_AVG"
		drop co2_phev

		

*Generate average emission and vmt for the average EFMP vehicle using subsidy counts as the weights

*For Panels A and B we use the VMT as calculated in the VMT formula
if `z' ==1 | `z' == 2 {
		*egen efmp_emission = wtmean(co2_emission), weight(efmp_count)			
		*egen efmp_vmt = wtmean(vmt), weight(efmp_count)	
		tempfile hello`z'
		save `hello`z''	
	}
	
*For panel C, we assume the difference between the EFMP and ICE mileage is supplanted by ICE emissions
else if `z' ==3 {
	egen efmp_emission = wtmean(co2_emission), weight(efmp_count)
	egen efmp_vmt = wtmean(vmt), weight(efmp_count)
	gen ice_vmt = `ice'
	gen vmt_diff = ice_vmt - efmp_vmt
	
	gen replacement_emission = (vmt_diff * `ice_gpm' * ice_co2_gallon)
	*replace co2_emission = ((efmp_emission * efmp_vmt)+(replacement_emission * vmt_diff))/(efmp_vmt+vmt_diff) if veh_type == "EFMP_avg"
	replace co2_emission = efmp_emission + replacement_emission if veh_type == "PHEV_AVG"
	tempfile hello3
	save `hello3'
	}
*************************************************************
*Scenario****************************************************
*************************************************************

*Compare EFMP Alternative Vehicle to different power generation scenario




		keep if veh_type == "PHEV_AVG" | veh_type == "Counterfactual" | veh_type == "New_car" | veh_type == "All_cars_bought"
		keep veh_type co2_emission
			gen i = 1
		reshape wide co2, i(i) j(veh_type) string

		foreach i in Counterfactual New_car All_cars_bought {
		gen saving_100_`i' = co2_emission`i' - co2_emissionPHEV_AVG
			summ saving_100_`i'
			local `i'_100_`z' = string(r(mean),"%10.0fc")
		gen saving_50_`i' = co2_emission`i' - co2_emissionPHEV_AVG/2
			summ saving_50_`i'
			local `i'_50_`z' = string(r(mean),"%10.0fc")
			}

restore
}
	
	use `hello3', clear
	
	*Tex Table of Results
	capture erase "$EFMPFleetRes/Implied_CO2_savings_phev.tex"
	file open fh using "$EFMPFleetRes/Implied_CO2_savings_phev.tex", write replace
	
	*Header
	file write fh ///
	"\begin{tabular}{l c c c}"  _n  ///
	"\toprule" _n ///
	" & \multicolumn{3}{c}{Reference Vehicle} \\ " _n ///
	" \cline{2-4} \\ " _n ///
	" &   & \multicolumn{1}{c}{Avg. new car} &  \multicolumn{1}{c}{Avg. new or used}  \\  " _n  ///
	"\multicolumn{1}{c}{Electric Generation Source}  & \multicolumn{1}{c}{Counterfactual} & \multicolumn{1}{c}{purchased} &  \multicolumn{1}{c}{car purchased}  \\  " _n  ///
	"\cline{1-4} \\ " _n
	
	*Panel A 	
	file write fh "\textit{Panel A: EV VMT = NHTS ICE} \\ " _n
	file write fh "\hspace{5 pt} Natural Gas  & `Counterfactual_100_1' & `New_car_100_1' & `All_cars_bought_100_1' \\ " _n
	file write fh "\hspace{5 pt}  50\% Natural Gas, 50\% Renewable  & `Counterfactual_50_1' & `New_car_50_1' & `All_cars_bought_50_1' \\ " _n
	file write fh " \\ " _n
	*Panel B 	
	file write fh "\textit{Panel B: EV VMT = NHTS EV VMT} \\ " _n
	file write fh "\hspace{5 pt} Natural Gas  & `Counterfactual_100_2' & `New_car_100_2' & `All_cars_bought_100_2' \\ " _n
	file write fh "\hspace{5 pt}  50\% Natural Gas, 50\% Renewable  & `Counterfactual_50_2' & `New_car_50_2' & `All_cars_bought_50_2' \\ " _n
	file write fh " \\ " _n
	*Panel C 	
	file write fh "\textit{Panel C: EV VMT = NHTS; Remainder NHTS ICE} \\ " _n
	file write fh "\hspace{5 pt} Natural Gas  & `Counterfactual_100_3' & `New_car_100_3' & `All_cars_bought_100_3' \\ " _n
	file write fh "\hspace{5 pt}  50\% Natural Gas, 50\% Renewable  & `Counterfactual_50_3' & `New_car_50_3' & `All_cars_bought_50_3' \\ " _n
	
	file write fh " \\ " _n
	file write fh "Reference Vehicle MPG & 35.0 & 27.48 & 24.87 \\ " _n
	file write fh "Reference Vehicle GPM & .0286 & .0364 & .0402 \\ " _n
	
	
	*Bottom 
	file write fh ///
	"\bottomrule  " _n ///
	"\end{tabular}"  _n _n _n _n _n
	
file close fh
macro drop fh






